package com.example.restatr.Atcivity.CitySubway;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.restatr.Atcivity.CitySubway.Bean.MetroLineInfoBean;
import com.example.restatr.Atcivity.CitySubway.Bean.MetroStepCurrentBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class MetroStepAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private TextView currentName;
    private TabLayout tab;
    private ListView lv;
    private List<MetroStepCurrentBean.DataBean> data = new ArrayList<>();//到站信息
    private ArrayList<MetroStepCurrentBean.DataBean> collect; //到站信息去重后数据，用于tab展示
    private List<MetroStepCurrentBean.DataBean> adapterData;//到站信息分类后数据，用于adapter数据展示
    private MetroLineInfoBean.DataBean lineInfoData = new MetroLineInfoBean.DataBean();//线路信息，用于展示首末时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_metro_step_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        currentName = findViewById(R.id.currentName);
        tab = findViewById(R.id.tab);
        lv = findViewById(R.id.listView);

        Tools.SetBarTitle(toolbar, barTitle, "到站信息", search, this,false);
        currentName.setText("当前站："+getIntent().getStringExtra("name"));
        GetMetroStep();
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                adapterData = data.stream().filter(r -> r.getLineName().equals(tab.getText().toString()) ).collect(Collectors.toList());
                lv.setAdapter(new adapter());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void GetMetroStep() {
        new Thread(() -> {
            String name = Tools.Get("/prod-api/api/metro/list?currentName=" + getIntent().getStringExtra("name"), "");
            MetroStepCurrentBean metroStepCurrentBean = new Gson().fromJson(name, MetroStepCurrentBean.class);
            data = metroStepCurrentBean.getData();
            runOnUiThread(() -> {
                if (data.size() > 0) {
                    collect = data.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(dataBean -> dataBean.getLineName()))), ArrayList::new));
                    for (int i = 0; i < collect.size(); i++) {
                        tab.addTab(tab.newTab().setText(collect.get(i).getLineName()));
                    }
                }
            });
        }).start();
    }

    //查询地铁站详情
    private void GetLineInfo(int pos,TextView startTime,TextView endTime) {
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/metro/line/" + pos, "");
            MetroLineInfoBean metroLineInfoBean = new Gson().fromJson(get, MetroLineInfoBean.class);
            lineInfoData = metroLineInfoBean.getData();
            runOnUiThread(() -> {
                startTime.setText(lineInfoData.getStartTime());
                endTime.setText(lineInfoData.getEndTime());
            });

        }).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return adapterData.size();
        }

        @Override
        public Object getItem(int position) {
            return adapterData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.metro_item_layout, null);
            TextView preStep = convertView.findViewById(R.id.preStep);
            TextView nextStep = convertView.findViewById(R.id.nextStep);
            TextView reachTime = convertView.findViewById(R.id.reachTime);
            TextView startTime = convertView.findViewById(R.id.startTime);
            TextView endTime = convertView.findViewById(R.id.endTime);

            if (adapterData.get(position).getPreStep()!=null){
                preStep.setText("上一站 " + adapterData.get(position).getPreStep().getName());
            }else {
                preStep.setText("上一站 " + "暂无");
            }
            if (adapterData.get(position).getNextStep()!=null){
                nextStep.setText("下一站 " + adapterData.get(position).getNextStep().getName());

            }else {
                nextStep.setText("下一站 " + "暂无");
            }
            reachTime.setText(adapterData.get(position).getReachTime() + "分钟");
            GetLineInfo(adapterData.get(position).getLineId(),startTime,endTime);
            return convertView;
        }
    }
}